import java.util.*;

public class Solution429 {

    static class Node {
        public int val;
        public List<Node> children;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    static class Pair{
        Node node;
        int deep;

        public Pair(Node node, int deep) {
            this.node = node;
            this.deep = deep;
        }
    }

    public List<List<Integer>> levelOrder(Node root) {
        if (root==null){
            return new ArrayList<>();
        }
        List<List<Integer>> ans=new ArrayList<>();
        Queue<Pair> queue=new LinkedList<>();
        queue.add(new Pair(root,0));
        while (!queue.isEmpty()){
            Pair pair=queue.poll();
            if (pair.deep==ans.size()){
                ans.add(new ArrayList<>());
            }
            ans.get(pair.deep).add(pair.node.val);
            if (pair.node.children!=null) {
                for (Node child : pair.node.children) {
                    queue.offer(new Pair(child, pair.deep + 1));
                }
            }
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution429().levelOrder(new Node(1, Arrays.asList(new Node(3,Arrays.asList(new Node(5),new Node(6))),new Node(2),new Node(4)))));
    }
}
